package cn.tedu.medicalappointmentsystemk.service.impl;

import cn.tedu.medicalappointmentsystemk.dao.persist.repository.PatientRepository;
import cn.tedu.medicalappointmentsystemk.dao.persist.repository.UserRepository;
import cn.tedu.medicalappointmentsystemk.handler.ServiceException;
import cn.tedu.medicalappointmentsystemk.pojo.bo.PatientSelectBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.PatientSelectDetailBO;
import cn.tedu.medicalappointmentsystemk.pojo.param.PatientUpdateParam;
import cn.tedu.medicalappointmentsystemk.pojo.po.PageData;
import cn.tedu.medicalappointmentsystemk.pojo.po.PatientPO;
import cn.tedu.medicalappointmentsystemk.pojo.po.UserPO;
import cn.tedu.medicalappointmentsystemk.pojo.vo.PatientUpdateVO;
import cn.tedu.medicalappointmentsystemk.response.ServiceCode;
import cn.tedu.medicalappointmentsystemk.service.PatientService;
import cn.tedu.medicalappointmentsystemk.util.PageInfoToPageDataConverter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    PatientRepository patientRepository;

    @Autowired
    UserRepository userRepository;

    @Value("${medical.dao.default-query-page-size}")
    private int pageSize;

    @Override
    public PatientSelectDetailBO patientDetail(Long id) {
        PatientPO patientPO=patientRepository.selectDetailById(id);
        Long userId=patientPO.getUserId();
        UserPO userPO = userRepository.selectById(userId);
        PatientSelectDetailBO patientSelectDetailBO=new PatientSelectDetailBO();
        BeanUtils.copyProperties(userPO,patientSelectDetailBO);
        BeanUtils.copyProperties(patientPO,patientSelectDetailBO);
        return patientSelectDetailBO;
    }

    @Override
    public PageData<PatientSelectBO> patientSelect(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Page<PatientSelectBO> page = patientRepository.select();
        PageInfo<PatientSelectBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public void patientDelete(Long id) {
        PatientPO po = patientRepository.selectDetailById(id);
        Integer row = patientRepository.deleteById(id);
        if (row != 1){
            String message = "删除失败!";
            log.error(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
        row = userRepository.deleteById(po.getUserId());
        if (row != 1){
            String message = "删除失败!";
            log.error(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }

    @Override
    public void patientUpdate(PatientUpdateParam param) {
        PatientUpdateVO vo = new PatientUpdateVO();
        BeanUtils.copyProperties(param,vo);
        String birthday = param.getBirthday();
        if (!"".equals(birthday)){
            if (!birthday.contains("T")){
                String patten = "yyyy-MM-dd HH:mm:ss";
                SimpleDateFormat sdf = new SimpleDateFormat(patten);
                try {
                    vo.setBirthday(sdf.parse(param.getBirthday()));
                }catch (ParseException e){
                    log.error("出现了ParseException,异常消息如下:" + e);
                    return;
                }
            }else {
                Instant instant = Instant.parse(birthday);
                vo.setBirthday(Date.from(instant));
            }
        }
        Integer row = patientRepository.update(vo);
        if (row == 0){
            String message = "更新失败!";
            log.error(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public PageData<PatientSelectBO> searchLastWeek(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Page<PatientSelectBO> page = patientRepository.searchLastWeek();
        PageInfo<PatientSelectBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<PatientSelectBO> searchLastMonth(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Page<PatientSelectBO> page = patientRepository.searchLastMonth();
        PageInfo<PatientSelectBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<PatientSelectBO> searchToday(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Page<PatientSelectBO> page = patientRepository.searchToday();
        PageInfo<PatientSelectBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public List<PatientSelectBO> patientSelect(String nickname) {
        return patientRepository.select(nickname);
    }
}
